package com.zbkj.admin.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zbkj.admin.service.AdminProjectHandleV2Service;
import com.zbkj.common.model.project.AireqTask;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.service.project.AireqTaskService;
import com.zbkj.service.service.project.CityWeightService;
import com.zbkj.service.service.project.ProjectInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Auther:
 * @Date: 2024/9/14 14:22
 * @Description:
 */
@Slf4j
@Component
public class ProjectJobTask {
    @Autowired
    private AireqTaskService aireqTaskService;
    @Autowired
    private AdminProjectHandleV2Service adminProjectHandleV2Service;
    @Autowired
    private ProjectInfoService projectInfoService;
    @Autowired
    private CityWeightService cityWeightService;

    /**
     * 自动发起AI估算请求任务
     * @return
     */
    @XxlJob("automAiEstimateCallTask")
    public ReturnT<String> automAiEstimateCallTask() {
        LambdaQueryWrapper<AireqTask> wrapper = new LambdaQueryWrapper();
        wrapper.eq(AireqTask::getDeleteFlag,0);
        wrapper.eq(AireqTask::getTaskStatus,0);
        wrapper.le(AireqTask::getUpdateTime, DateUtil.addSecond(new Date(), -30));
        wrapper.le(AireqTask::getRetryNum, 3);
        wrapper.last(" limit 1000 ");
        List<AireqTask> taskList = aireqTaskService.list(wrapper);
        for (AireqTask task : taskList) {
            int status = 0;
            String msg = "";
            try {
                status = adminProjectHandleV2Service.aiAutomCallTask(task);
            } catch (Exception e) {
                msg = e.getMessage();
                log.error("automAiEstimateCallTask-taskId:{}-projectId:{}-error:",task.getId(),task.getProjectId(),e);
            }
            try {
                if(status != 1) {
                    //请求失败-更新任务状态|失败原因|尝试次数
                    aireqTaskService.updateTaskStatus(task.getId(),status,msg);
                }
            } catch (Exception e){

            }
        }
        log.info("automAiEstimateCallTask-run-size:{}",taskList.size());
        return ReturnT.SUCCESS;
    }

    @XxlJob("demoProjectAutoHandleTask")
    public ReturnT<String> demoProjectAutoHandleTask() {
        List<String> fileNameList = Lists.newArrayList("快递管家PC端.docx","临床思维综合训练系统软件需求分析.docx");
        Date delayDate = DateUtil.addSecond(new Date(), -20);
        List<Long> projectIdList = projectInfoService.getHandleDemoPrjectIds(fileNameList, delayDate);
        for(Long projectId : projectIdList) {
            try {
                adminProjectHandleV2Service.demoProjectAutoHandleTask(projectId);
            } catch (Exception e) {
                log.error("demoProjectAutoHandleTask-projectId:{}-处理异常:{}",projectId,e.getMessage());
            }
        }
        log.info("demoProjectAutoHandleTask-run-size:{}",projectIdList.size());
        return ReturnT.SUCCESS;
    }


    /**
     * 按市人工月费用缓存刷新任务
     * @return
     */
    @XxlJob("cityWeightCacheRefreshTask")
    public ReturnT<String> cityWeightCacheRefreshTask() {
        String result = "success";
        try {
            cityWeightService.cityWeightCacheRefresh(1);
        } catch (Exception e) {
            result = "fail";
            log.error("cityWeightCacheRefreshTask-error:",e);
        }
        log.error("cityWeightCacheRefreshTask-run-{}",result);
        return ReturnT.SUCCESS;
    }
}
